package org.ouwh.oowiki.http;

import java.security.AccessControlException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jetty.util.log.Logger;

public class JettyLog implements Logger {
	private final static boolean __debug = Boolean.parseBoolean(System.getProperty("org.eclipse.jetty.util.log.DEBUG",
			System.getProperty("org.eclipse.jetty.util.log.stderr.DEBUG", "false")));
	private final static boolean __source = Boolean.parseBoolean(System.getProperty("org.eclipse.jetty.util.log.SOURCE",
			System.getProperty("org.eclipse.jetty.util.log.stderr.SOURCE", "false")));

	private boolean _debug = __debug;
	private boolean _source = __source;
	private final String _name;
	private boolean _hideStacks = false;
	private Log log;

	public JettyLog() {
		this("org.ouwh.oowiki.http.jetty");
	}

	public JettyLog(String name) {
		this._name = name == null ? "" : name;

		try {
			_debug = Boolean.parseBoolean(System.getProperty(_name + ".DEBUG", Boolean.toString(_debug)));
		} catch (AccessControlException ace) {
			_debug = __debug;
		}

		try {
			_source = Boolean.parseBoolean(System.getProperty(_name + ".SOURCE", Boolean.toString(_source)));
		} catch (AccessControlException ace) {
			_source = __source;
		}
		log = LogFactory.getLog(name);
	}

	public String getName() {
		return _name;
	}

	public boolean isHideStacks() {
		return _hideStacks;
	}

	public void setHideStacks(boolean hideStacks) {
		_hideStacks = hideStacks;
	}

	public boolean isSource() {
		return _source;
	}

	public void setSource(boolean source) {
		_source = source;
	}

	public void warn(String msg, Object... args) {
		StringBuffer sb = new StringBuffer();
		for (Object o : args) {
			sb.append(o == null ? null : o.toString());
		}
		log.warn(msg + sb.toString());
	}

	public void warn(Throwable thrown) {
		log.warn("", thrown);
	}

	public void warn(String msg, Throwable thrown) {
		log.warn(msg, thrown);
	}

	public void info(String msg, Object... args) {
		StringBuffer sb = new StringBuffer();
		for (Object o : args) {
			sb.append(o == null ? null : o.toString());
		}
		log.info(msg + sb.toString());
	}

	public void info(Throwable thrown) {
		log.info("", thrown);
	}

	public void info(String msg, Throwable thrown) {
		log.info(msg, thrown);
	}

	public boolean isDebugEnabled() {
		return _debug;
	}

	public void setDebugEnabled(boolean enabled) {
		_debug = enabled;
	}

	public void debug(String msg, Object... args) {
		if (!_debug)
			return;
		StringBuffer sb = new StringBuffer();
		for (Object o : args) {
			sb.append(o == null ? null : o.toString());
		}
		log.debug(msg + sb.toString());
	}

	public void debug(Throwable thrown) {
		if (!_debug)
			return;
		log.debug("", thrown);
	}

	public void debug(String msg, Throwable thrown) {
		if (!_debug)
			return;
		log.debug(msg, thrown);
	}

	public Logger getLogger(String name) {
		if ((name == null && this._name == null) || (name != null && name.equals(this._name)))
			return this;
		return new JettyLog(_name == null || _name.length() == 0 ? name : _name + "." + name);
	}

	@Override
	public String toString() {
		return "JettyLog: " + _name + " DEBUG: " + _debug + " ";
	}

	@Override
	public void info(String msg) {
		log.info(msg);
	}

	@Override
	public void info(String msg, Object arg0, Object arg1) {
		arg0 = arg0 == null ? "" : ", " + arg0;
		arg1 = arg1 == null ? "" : ", " + arg1;
		log.info(msg + arg0 + arg1);
	}

	@Override
	public void debug(String msg) {
		if (!_debug)
			return;
		log.debug(msg);
	}

	@Override
	public void debug(String msg, Object arg0, Object arg1) {
		if (!_debug)
			return;
		arg0 = arg0 == null ? "" : ", " + arg0;
		arg1 = arg1 == null ? "" : ", " + arg1;
		log.debug(msg + arg0 + arg1);
	}

	@Override
	public void warn(String msg) {
		log.warn(msg);
	}

	@Override
	public void warn(String msg, Object arg0, Object arg1) {
		arg0 = arg0 == null ? "" : ", " + arg0;
		arg1 = arg1 == null ? "" : ", " + arg1;
		log.warn(msg + arg0 + arg1);
	}
}
